package com.yanqu.road.server.manger.activity.evefeast.service;

import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.dao.impl.player.UserEveFeastDaoImpl;
import com.yanqu.road.dao.impl.player.UserSilkRoadDaoImpl;
import com.yanqu.road.entity.activity.evefeast.EveFeastItem;
import com.yanqu.road.entity.activity.evefeast.UserEveFeastData;
import com.yanqu.road.entity.activity.silkroad.UserSilkRoadData;
import com.yanqu.road.logic.pb.EveFeastPb;
import com.yanqu.road.pb.activity.EveFeastProto;
import com.yanqu.road.pb.activity.SilkRoadProto;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.server.game.service.IGameServiceCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.evefeast.EveFeastModule;
import com.yanqu.road.server.gameplayer.module.activity.silkroad.SilkRoadModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.sync.message.SyncResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class EveFeastWatchMaxScoreItemsServiceCmd implements IGameServiceCommand {
    @Override
    public SyncResponse execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        SyncResponse syncResponse = new SyncResponse();
        EveFeastProto.EveFeastWatchMaxScoreItemsReqMsg reqMsg = EveFeastProto.EveFeastWatchMaxScoreItemsReqMsg.parseFrom(request.getDataBytes());

        UserEveFeastData userData = null;
        GamePlayer targetPlayer = GamePlayerMgr.getOnlinePlayer(reqMsg.getUserId());
        if (targetPlayer == null) {
            userData = new UserEveFeastDaoImpl().getUserEveFeastData(reqMsg.getActivityId(), reqMsg.getUserId());
        } else {
            userData = targetPlayer.getModule(EveFeastModule.class).getUserData();
        }
        EveFeastProto.EveFeastWatchMaxScoreItemsRespMsg.Builder respMsg = EveFeastProto.EveFeastWatchMaxScoreItemsRespMsg.newBuilder();
        if (userData != null) {
            respMsg.setRet(0);
            respMsg.setUserId(reqMsg.getUserId());
            List<EveFeastItem> maxScoreItems = new ArrayList<>(userData.getMaxScoreItems());
            for (EveFeastItem item : maxScoreItems) {
                respMsg.addItems(EveFeastPb.parseGameItemTempMsg(item));
            }
        }else{
            respMsg.setRet(GameErrorCode.E_USER_NO_FOUND);
        }
        syncResponse.setResponseBuilder(respMsg);
        return syncResponse;
    }
}
